package com.yuke.cloud.service.omc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.omc.dto.OdsAwardDto;
import com.yuke.cloud.service.omc.entity.OdsAward;
import com.yuke.cloud.service.omc.service.OdsAwardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by zn on 2020/10/16.
*/
@Api(value = "WEB - OdsAwardController", tags = "处理OdsAward相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/award")
public class OdsAwardController extends BaseController {
    @Resource
    private OdsAwardService odsAwardService;
    @Resource
    private PmcFeignApi pmcFeignApi;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "odsAward", value = "该数据记录的POJO") @RequestBody OdsAward odsAward) {
        odsAward.setAwardId(generateId());
        odsAwardService.save(odsAward);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        odsAwardService.deleteAwardById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "odsAward", value = "需要修改的内容") @RequestBody OdsAward odsAward) {
        odsAwardService.update(odsAward);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<OdsAward> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        OdsAward odsAward = odsAwardService.findById(id);
        return WrapMapper.ok(odsAward);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsAward>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<OdsAward> list = odsAwardService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/getAwardList")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper<List<OdsAwardDto>> getAwardList() {
        List<OdsAwardDto> list = odsAwardService.getAwardList();

        if (list == null || list.size() < 1){
            return WrapMapper.error("未找到请求的数据！");
        }
        List<Long> specId = list.stream().map(OdsAwardDto::getSpecId).collect(Collectors.toList());
        List<ProdSpecInfoDto> prodSpecInfoDtos  =pmcFeignApi.getSpecInfoBySpecIds(specId);
        if (prodSpecInfoDtos == null || prodSpecInfoDtos.size() < 1){
            return WrapMapper.ok(list);
        }

        for (OdsAwardDto odsAward : list){
            for(ProdSpecInfoDto prodSpecInfoDto : prodSpecInfoDtos){
                if (odsAward.getSpecId().equals(prodSpecInfoDto.getSpecId())){
                    odsAward.setPicUrl(prodSpecInfoDto.getBigPic());
                }
            }
        }
        return WrapMapper.ok(list);
    }
}
